public class PersonAccountHomeLoanController {
    
    public List<accountwrapper> wrapper{get;set;}
    public List<accountwrapper> wrapperlist{get;set;}
    public String inputvalue{get;set;}
    public List<Account> accountfilterlist{get;set;}
    public Set<Id> accountids{get;set;}

    public PersonAccountHomeLoanController() {
        getwrapperlist();
    }
           
    public class accountwrapper{    
        
        public String Accountid{get;set;}
        public String AccountName{get;set;}
        public String mobilenumber{get;set;}
        
        public Opportunity opp{ get; set;}
 
        public accountwrapper(){
            opp = new Opportunity();
            
        }
        
        public accountwrapper(Opportunity wrapper){
            Opp = wrapper;
        }
        
    }
   
    
     public PageReference ViewAll() {     
         getwrapperlist();
         return null;
    }


    public PageReference Searchbyname() {
    
    try{

     if(inputvalue!=null && inputvalue!=''){
     
             inputvalue='%'+inputvalue+'%';
             String Querystring='Select Id,Name,FirstName,LastName,H_Mobile_No__c from Account where ((Firstname LIKE '+'\''+inputvalue+'\''+') OR (LastName Like '+'\''+inputvalue+'\''+') OR (Name Like '+'\''+inputvalue+'\''+') OR (H_Mobile_No__c LIKE '+'\''+inputvalue+'\''+')) AND (ID IN: accountids )';     
             accountfilterlist=Database.Query(Querystring);                
             wrapperlist=new List<accountwrapper>();
             
          for(Account acc: accountfilterlist)
            {                            
                accountwrapper wrapper=new accountwrapper();
                wrapper.Accountid=acc.Id;
                wrapper.AccountName=acc.Name;
                wrapper.mobilenumber=acc.H_Mobile_No__c;
                wrapperlist.add(wrapper);            
            }   
       
          }else{         
               getwrapperlist();      
          }
          }catch(Exception e){
           return null;
          }
          
          inputvalue=inputvalue.replace('%','');
        return null;
    }
    
    
    // instantiate the StandardSetController from a query locator
    public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, AccountId, Account.Name,Amount, Account.RecordType.Name,Booking_Date__c,Account.H_Mobile_No__c,
        Oasys_Flat_Code__c,Stage__c,OASYS_Status__c from Opportunity 
        where Account.RecordType.Name =: 'Person Account' AND Booking_Date__c!=null AND ( (StageName='Closed Won') OR (StageName='Closed Book'))
        AND Oasys_Flat_Code__c !=null AND ( (OASYS_Status__c='Sold')OR(OASYS_Status__c='Booking')) order by Name]));
                
                // sets the number of records in each page set
                con.setPageSize(10);
                
                
            }
            return con;
        }
        set;
    }
    
       
    // returns a list of wrapper objects for the sObjects in the current page set
    public List<accountwrapper> getwrapperlist() {
    
        system.Debug('## Inside getwrapperlist');    
        system.Debug('## Inside getwrapperlist : '+(List<Opportunity>)con.getRecords());    
    
        try{
        
        
        wrapperlist=new List<accountwrapper>();
            accountids=new Set<Id>();   
          
        for(Opportunity Opp: (List<Opportunity>)con.getRecords())
            {        
            if(!accountids.contains(opp.AccountId)){
                         
                accountwrapper wrapper=new accountwrapper();
                wrapper.Accountid=Opp.AccountId;
                accountids.add(Opp.AccountId);
                wrapper.AccountName=Opp.Account.Name;
                wrapper.mobilenumber=opp.Account.H_Mobile_No__c;
                
                wrapperlist.add(wrapper);
              }
            
            }
           
        }catch(Exception e){
        
        system.Debug('## Exception e :'+ e);  
            return null;
        }    
        system.Debug('## wrapperlist :'+wrapperlist); 
         
        return wrapperlist;
   
    }
 
    
    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            system.Debug('## wrapperlist :'+wrapperlist);
            return con.getHasNext();
            
        }
        set;
    }
 
    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            system.Debug('## wrapperlist :'+wrapperlist);
            return con.getHasPrevious();
            
        }
        set;
    }
 
    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
 
    // returns the first page of records
    public void first() {
        con.first();
    }
 
    // returns the last page of records
    public void last() {
        con.last();
    }
 
    // returns the previous page of records
    public void previous() {
        con.previous();
    }
 
    // returns the next page of records
    public void next() {
        con.next();
    }
 

}